from db_connect import ConnMysqlSlave
from db_connect import ConnMysql
import aiomysql


class Order():

    def __init__(self, user_id, merchat_id):

        self.user_id = user_id
        self.merchat_id = merchat_id
        # self.result = self.main()


    async def result(self):

        #判断所传参数是否为空
        if not (self.user_id and self.merchat_id):

            return {
                'status': -1,
                'errorCode': 701,
                'errorMsg': '参数不能为空',
                'resultBody': None,
            }
        #参数必须为数字
        if not (self.user_id.isdigit() and self.merchat_id.isdigit()):

            return {
                'status': -1,
                'errorCode': 1102,
                'errorMsg': '参数必须为数字',
                'resultBody': None,
            }
        
        #从进程池里取链接对象
        async with ConnMysqlSlave().pool.acquire() as c:

            #检测连接对象是否连接
            await c.ping()
            #游标的上下文管理
            async with c.cursor(aiomysql.DictCursor) as cur:

                sql = "SELECT * FROM orders WHERE user_id=%s AND merchat_id=%s "
                await cur.execute(sql, (self.user_id,self.merchat_id))
                select_info = await cur.fetchall()

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': select_info,
        }